// pages/home/partner/partner.js
import {
  getStorageItem
} from '../../../utils/storage'
import { formatNoZeroDate } from '../../../utils/date'
import { getHhrInfo1 , getHhrincominfo } from '../../../api/income'

const getToday = () => {
  var month = new Date()
  month.setMonth(month.getMonth() - 1)
  return formatNoZeroDate(`${month.getFullYear()}-${month.getMonth()+1}`)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hhrType: getStorageItem("hhr_type"),
    info1: null,
    info2: null,
    month: getToday(),
    info: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const hhr_type = getStorageItem("hhr_type")
    this.setData(
     { hhr_type}
    )
    if(hhr_type=='0'){
      return 
    }
    this.getHhrInfo1()
    this.getHhrincominfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  ,
  getHhrInfo1(){
 
      getHhrInfo1().then(res => {
        
        let {info1 , info2}  = res.data
        this.setData({
          info1,
          info2,
        })
      })
      .catch(err => {
        
      })
 
  },
  getHhrincominfo(){
    getHhrincominfo({
      month: getToday(),
    })
    .then( res => {
      
      let {info}  = res.data

      if(info.gap_income){
        let gap_income = Number(info.gap_income)
        info.gap_income_abs = Math.abs(gap_income)
        info.gap_income_class  = info.gap_income>0?'increase':'decrease'
        let gap_income_string  = info.gap_income>0?'+':'-'

        info.gap_income_string  = `${gap_income_string}${info.gap_income_abs}`
 
      }
      this.setData({
        info
      })
      
    })
    .catch(err => {
      
    })
  }
})